import {Component, OnDestroy, OnInit} from '@angular/core';
import {FormArray, FormBuilder, FormControl, FormGroup} from '@angular/forms';
import {CommonService} from '../../../shared/service/common.service';
import {Subscription} from 'rxjs';

@Component({
  selector: 'app-form-group',
  templateUrl: './form-group.component.html',
  styleUrls: ['./form-group.component.scss']
})
export class FormGroupComponent implements OnInit, OnDestroy {

  formGroupInst: object = this.commonService.formGroupInst;
  attrOutput: string = '';
  methodOutput: string = '';
  formGroupSubscription = new Subscription();

  constructor(
    private fb: FormBuilder,
    private commonService: CommonService
  ) { }

  ngOnInit() {
    this.listenEvents();
  }

  ngOnDestroy() {
    this.formGroupSubscription.unsubscribe();
  }

  /**
   * 获取FormGroup的value值
   */
  getValue() {
    this.attrOutput = this.formGroupInst['name'] + '控件的value值为： ' + this.commonService.convertType(this.formGroupInst['instantiation'].value);
  }

  /**
   * 获取父控件
   */
  getParentControl() {
    this.attrOutput = '请打开控制台查看输出信息';
    console.log(name + '的父控件是： ');
    console.log(this.formGroupInst['instantiation'].parent);
  }

  /**
   * 获取FormGroup控件的valid值
   */
  getValid() {
    this.attrOutput = this.formGroupInst['name'] + '的valid值为： ' + this.formGroupInst['instantiation'].valid;
  }

  /**
   * 获取FormGroup控件的invalid值
   */
  getInValid() {
    this.attrOutput = this.formGroupInst['name'] + '的invalid值为： ' + this.formGroupInst['instantiation'].invalid;
  }

  /* 以下是方法列表 */
  /**
   * 设置FormGroup控件的同步校验器
   */
  setFormGroupValidators() {
    // this.selectedFormObj['FormGroup'].setValidators(forbiddenNameValidator(/sowhat/i));
  }

  /**
   * 监听subscribe事件
   */
  listenEvents() {
    this.formGroupSubscription = this.commonService.formGroupSubject.subscribe(formGroupObj => {
      this.formGroupInst = formGroupObj;
    });
  }

}
